<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class GoodsController extends Controller
{
    /**
     * 发布找货信息
     *
     * @param  Request
     * @return Response
     */
    public function releaseGoods(Request $request)
    {
        try{
            $data['users_id'] = $request->input('userId', '');
            $data['name'] = $request->input('name', '');
            $data['pic'] = $request->input('pic', null);
            $data['pay_diamond'] = $request->input('diamond', 5);
            $data['category'] = $request->input('category', 0);
            $data['country_id'] = $request->input('country', 0);
            $data['province_id'] = $request->input('province', 0);
            $data['city_id'] = $request->input('city', 0);
            $data['area_id'] = $request->input('area', 0);
            $data['search_goods'] = 0;
            if(!empty($data['country_id'])){
                $data['search_goods'] = $data['country_id'];
            }
            if(!empty($data['province_id'])){
                $data['search_goods'] = $data['province_id'];
            }
            if(!empty($data['city_id'])){
                $data['search_goods'] = $data['city_id'];
            }
            if(!empty($data['area_id'])){
                $data['search_goods'] = $data['area_id'];
            }
            $data['create_time'] = time();

            if(empty($data['users_id'])){
                return response()->json(['success'=>false,'code'=>2001,'message'=>'用户userID为空']);
            }
            if(empty($data['name'] )){
                return response()->json(['success'=>false,'code'=>2002,'message'=>'找货信息为空']);
            }
            //判断当前用户是否已完善所有用户信息
            if(!DB::table('diamond_use')->where([['users_id','=',$data['users_id']],['diamond_type','=','2']])->value('id')){
                return response()->json(['success'=>false,'code'=>2031,'message'=>'完善个人信息后才能发布找货信息']);
            }
            $setting = DB::table('diamond_setting')->first();
            //判断当前用户钻石是否满足
            if($data['pay_diamond'] < $setting->find_goods_apply){
                return response()->json(['success'=>false,'code'=>2003,'message'=>'找货悬赏最少'.$setting->find_goods_apply.'钻石']);
            }
            $userDiamonds = DB::table('users')->where('id', $data['users_id'])->value('diamonds');
            if(!$userDiamonds || $data['pay_diamond'] > $userDiamonds){
                return response()->json(['success'=>false,'code'=>2004,'message'=>'用户钻石数量不足']);
            }
            //修改用户钻石数
//            $updateData['diamonds'] = $userDiamonds - $data['pay_diamond'];
            DB::beginTransaction();
            $insert = DB::table('goods')->insert($data);
            $update = DB::table('users')->where('id',$data['users_id'])->decrement('diamonds', $data['pay_diamond']);
            //增加发布记录
//            $binviterAddDiamond = DB::table('users')->where('id','=',$data['users_id'])->increment('diamonds', $setting->find_goods_apply);
            $binviterUseDiamond = DB::table('diamond_use')->insert(array('users_id'=>$data['users_id'],'diamond'=>$setting->find_goods_apply,'diamond_type'=>11,'create_time'=>time()));
            if($insert && $update && $binviterUseDiamond){
                DB::commit();
                return response()->json(['success'=>true,'code'=>1000,'message'=>'发布成功']);
            }else{
                DB::rollBack();
                return response()->json(['success'=>false,'code'=>2005,'message'=>'发布失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 删除找货信息
     *
     * @param  Request
     * @return Response
     */
    public function delGoods(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            $goodsId = $request->input('goodsId', '');
            if(empty($userId) || empty($goodsId)){
                return response()->json(['success'=>false,'code'=>2006,'message'=>'必填参数不能为空']);
            }
            $data['is_del'] = 1;
            $data['del_time'] = time();
            $update = DB::table('goods')->where([
                ['id','=',$goodsId],
                ['users_id','=',$userId]
            ])->update($data);
            if($update){
                return response()->json(['success'=>true,'code'=>1000,'message'=>'删除成功']);
            }else{
                return response()->json(['success'=>false,'code'=>2008,'message'=>'删除失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 举报找货信息
     *
     * @param  Request
     * @return Response
     */
    public function reportGoods(Request $request)
    {
        try{
            $data['users_id'] = $request->input('userId', '');
            $data['goods_id'] = $request->input('goodsId', '');
            $data['message'] = $request->input('message', '');
            $data['status'] = 2;
            $data['create_time'] = time();
            if(empty($data['users_id']) || empty($data['goods_id'])){
                return response()->json(['success'=>false,'code'=>2009,'message'=>'必填参数不能为空']);
            }
            $data['busers_id'] = DB::table('goods')->where('id','=',$data['goods_id'])->value('users_id');
            $insert = $insert = DB::table('user_message')->insert($data);
            if($insert){
                return response()->json(['success'=>true,'code'=>1000,'message'=>'举报成功']);
            }else{
                return response()->json(['success'=>false,'code'=>2011,'message'=>'举报失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 评论找货信息
     *
     * @param  Request
     * @return Response
     */
    public function commentGoods(Request $request)
    {
        try{
            $data['users_id'] = $request->input('userId', '');
            $data['goods_id'] = $request->input('goodsId', '');
            $data['message'] = $request->input('message', '');
            $data['price'] = $request->input('price', 0);
            $data['pay_diamond'] = $request->input('diamond', 0);
            $data['status'] = 1;
            $data['create_time'] = time();
            $share = $request->input('share', 0);
            if(empty($data['users_id']) || empty($data['goods_id'])){
                return response()->json(['success'=>false,'code'=>2012,'message'=>'必填参数不能为空']);
            }
            //判断当前用户是否已完善所有用户信息
            if(!DB::table('diamond_use')->where([['users_id','=',$data['users_id']],['diamond_type','=','2']])->value('id')){
                return response()->json(['success'=>false,'code'=>2030,'message'=>'完善个人信息后才能留言']);
            }
            $setting = DB::table('diamond_setting')->first();
            //判断当前用户钻石是否满足
            if($data['pay_diamond'] < $setting->confirm_the_goods){
                return response()->json(['success'=>false,'code'=>2012,'message'=>'确认有货最少'.$setting->confirm_the_goods.'钻石']);
            }
            $data['busers_id'] = DB::table('goods')->where('id','=',$data['goods_id'])->value('users_id');
            //判断支付的钻石是否够
            $info = DB::table('users')->where('id', $data['users_id'])->first();
            if(!$info->diamonds || $data['pay_diamond'] > $info->diamonds){
                return response()->json(['success'=>false,'code'=>2013,'message'=>'用户钻石数量不足']);
            }
            DB::beginTransaction();
            //判断1、分享后好友确认有货，2、确认有货，3、我邀请的人付出钻石（百分比）
            //判断是否是分享
            $addDiamond = true;
            $useDiamond = true;
            if($share == 1){
                $useData['users_id'] = $data['busers_id'];
                $useData['create_time'] = time();
                $useData['diamond_type'] = 5;
                $useData['diamond'] = $setting->share_friend_in_stock;
                $addDiamond = DB::table('users')->where('id','=',$useData['users_id'])->increment('diamonds', $useData['diamond']);
                $useDiamond = DB::table('diamond_use')->insert($useData);
                $isShare = array(
                    'diamond'=>$useData['diamond'],
                    'userId'=>$useData['users_id'],
                    'nick_name'=>DB::table('users')->where('id','=',$useData['users_id'])->value('nick_name'),
                    'znick_name'=>$info->nick_name
                );
            }
            //判断是否存在邀请人
            $inviterAddDiamond = true;
            $inviterUseDiamond = true;
            if($info->inviter_id){
                $inviterDiamond = $setting->invite_pay * $data['pay_diamond'] / 100;
                $inviterAddDiamond = DB::table('users')->where('id','=',$info->inviter_id)->increment('diamonds', $inviterDiamond);
                $inviterUseDiamond = DB::table('diamond_use')->insert(array('users_id'=>$info->inviter_id,'diamond'=>$inviterDiamond,'diamond_type'=>10,'create_time'=>time()));
                $isYao = array(
                    'diamond'=>$inviterDiamond,
                    'userId'=>$info->inviter_id,
                    'nick_name'=>DB::table('users')->where('id','=',$info->inviter_id)->value('nick_name'),
                );
            }
            //添加信息，扣除钻石，添加扣除记录
            $insert = DB::table('user_message')->insert($data);
            $update = DB::table('users')->where('id',$data['users_id'])->decrement('diamonds', $data['pay_diamond']);
            $updateMsg = DB::table('diamond_use')->insert(array('users_id'=>$data['users_id'],'diamond'=>$data['pay_diamond'],'diamond_type'=>12,'create_time'=>time()));
            if($insert && $update && $addDiamond && $useDiamond && $updateMsg && $inviterAddDiamond && $inviterUseDiamond){
                DB::commit();
                //发送消息1.评论消息，2钻石消息
                $zname = DB::table('goods')->where('id','=',$data['goods_id'])->value('name');
                if(self::SendTemplate($data['busers_id'],1,'「'.$data['pay_diamond'].'💎」'.$data['message'],$info->nick_name,$zname,date("m-d H:i",$data['create_time']),$data['goods_id']) == 1004){
                    self::SendTemplate($data['busers_id'],1,'「'.$data['pay_diamond'].'💎」'.$data['message'],$info->nick_name,$zname,date("m-d H:i",$data['create_time']),$data['goods_id']);
                }
                if(!empty($isShare)){
                    //发送分享钻石消息
                    if(self::SendTemplate($isShare['userId'],3,'【分享后好友确认有货】钻⽯奖励',$isShare['diamond'].'💎','分享后好友确认有货：你分享「'.$isShare['nick_name'].'」的找货信息被「'.$isShare['znick_name'].'」留⾔有货',date("m-d H:i")) == 1004){
                        self::SendTemplate($isShare['userId'],3,'【分享后好友确认有货】钻⽯奖励',$isShare['diamond'].'💎','分享后好友确认有货：你分享「'.$isShare['nick_name'].'」的找货信息被「'.$isShare['znick_name'].'」留⾔有货',date("m-d H:i"));
                    }
                }
                if(!empty($isYao)){
                    //发送邀请钻石消息
                    if(self::SendTemplate($isYao['userId'],3,'【我邀请的⼈付出钻⽯】奖励',$isYao['diamond'].'💎','我邀请的⼈付出钻⽯：您邀请的「'.$isYao['nick_name'].'」付出了钻⽯',date("m-d H:i")) == 1004){
                        self::SendTemplate($isYao['userId'],3,'【我邀请的⼈付出钻⽯】奖励',$isYao['diamond'].'💎','我邀请的⼈付出钻⽯：您邀请的「'.$isYao['nick_name'].'」付出了钻⽯',date("m-d H:i"));
                    }
                }
                return response()->json(['success'=>true,'code'=>1000,'message'=>'评论成功']);
            }else{
                DB::rollBack();
                return response()->json(['success'=>false,'code'=>2015,'message'=>'评论失败']);
            }
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 找货信息搜索列表
     *
     * @param  Request
     * @return Response
     */
    public function keylistGoods(Request $request)
    {
        try{
            $where[] = ['goods.is_del','=','2'];
            $keyword = $request->input('keyword', '');
            if(!empty($keyword)){
                $where[] = ['goods.name','like','%'.$keyword.'%'];
            }
            //当前页码
            $pageNumber = $request->input('pageNumber', 1);
            //每页显示数量
            $pageSize = $request->input('pageSize', 10);
            $pyl = ($pageNumber-1)*$pageSize;
            $res = DB::table('goods')->select('goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name')
                ->join('users', 'goods.users_id', '=', 'users.id')
                ->where($where)->orderBy('goods.id', 'desc')->skip($pyl)->take($pageSize)->get();
//            if(!empty($res)){
//                $oss = DB::table('oss_format')->first();
//                foreach ($res as $k=>$v){
//                    $pic = explode('|',$v->pic);
//                    if(!empty($pic)){
//                        foreach($pic as $kk=>$vv){
//                            $pic[$kk] = $vv.'?x-oss-process=style/'.$oss->info_pic;
//                        }
//                    }
//                    $v->pic = implode('|',$pic);
//                    $v->head_pic = $v->head_pic.'?x-oss-process=style/'.$oss->original_pic;
//                }
//            }
            return response()->json(['success'=>true,'code'=>1000,'list'=>$res]);
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 获取找货列表
     *
     * @param  Request
     * @return Response
     */
    public function listGoods(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            $isMy = $request->input('isMy', false);
            $lat = $request->input('lat', '');
            $lng = $request->input('lng', '');
            $category = $request->input('category', '');
            $diamondBottom = $request->input('diamondBottom', '');
            $diamondHigh = $request->input('diamondHigh', '');
            $pageNumber = $request->input('pageNumber', 1);
            $pageSize = $request->input('pageSize', 10);
            $keyword = $request->input('keyword', '');
            if(empty($userId)){
                return response()->json(['success'=>false,'code'=>2016,'message'=>'必填参数不能为空']);
            }
            $where[] = ['goods.is_del','=','2'];
            $orWhere[] = ['goods.is_del','=','2'];
            $orWhere[] = ['goods.users_id','=',$userId];
            $whereBeween = [];
            if($isMy == 'true'){
                $where[] = ['goods.users_id','=',$userId];
            }
            if(!empty($category)){
                $where[] = ['goods.category','=',$category];
                $orWhere[] = ['goods.category','=',$category];
            }
            if(!empty($keyword)){
                $where[] = ['goods.name','like','%'.$keyword.'%'];
                $orWhere[] = ['goods.name','like','%'.$keyword.'%'];
            }
            if(!empty($diamondBottom) && empty($diamondHigh)){
                $where[] = ['goods.pay_diamond','>=',$diamondBottom];
                $orWhere[] = ['goods.pay_diamond','>=',$diamondBottom];
            }elseif (empty($diamondBottom) && !empty($diamondHigh)){
                $where[] = ['goods.pay_diamond','<=',$diamondHigh];
                $orWhere[] = ['goods.pay_diamond','<=',$diamondHigh];
            }elseif (!empty($diamondBottom) && !empty($diamondHigh)){
                $whereBeween[] = [$diamondBottom,$diamondHigh];
            }
            //查询当前用户店铺设置
            $info = DB::table('users')->where('id','=',$userId)->first();
            $whereIn = [0,$info->position_country,$info->position_province,$info->position_city,$info->position_area];
            //解析地址
            if(!empty($lat) && !empty($lng)){
                $address = self::httpGet('https://apis.map.qq.com/ws/geocoder/v1/?location='.$lat.','.$lng.'&key=N2IBZ-FGZA4-QQBUU-XNESJ-FIY2Z-YOFLM&get_poi=1');
                $address = json_decode($address);
                $countryId = DB::table('base_country')->where('name', 'like', '%'.$address->result->address_component->nation.'%')->value('id');
                $provinceId = DB::table('base_province')->where('name', 'like', '%'.$address->result->address_component->province.'%')->value('code');
                $cityId = DB::table('base_city')->where('name', 'like', '%'.$address->result->address_component->city.'%')->value('code');
                $areaId = DB::table('base_area')->where('name', 'like', '%'.$address->result->address_component->district.'%')->value('code');
                $whereIn = array_merge($whereIn,[0,$countryId,$provinceId,$cityId,$areaId]);
            }
            $pyl = ($pageNumber-1)*$pageSize;
            if(!empty($whereBeween) && empty($whereIn)){
                $res = DB::table('goods')->select('base_area.name as area_name','base_city.name as city_name','base_province.name as province_name','base_country.name as country_name','goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name')
                    ->join('users', 'goods.users_id', '=', 'users.id')
                    ->leftJoin('base_category', 'goods.category', '=', 'base_category.id')
                    ->leftJoin('base_country', 'goods.country_id', '=', 'base_country.id')
                    ->leftJoin('base_province', 'goods.province_id', '=', 'base_province.code')
                    ->leftJoin('base_city', 'goods.city_id', '=', 'base_city.code')
                    ->leftJoin('base_area', 'goods.area_id', '=', 'base_area.code')
                    ->where($where)
                    ->whereBetween('goods.pay_diamond',$whereBeween)
                    ->orWhere($orWhere)
                    ->orderBy('goods.id', 'desc')->skip($pyl)->take($pageSize)->get();
            }elseif(!empty($whereIn) && empty($whereBeween)){
                $res = DB::table('goods')->select('base_area.name as area_name','base_city.name as city_name','base_province.name as province_name','base_country.name as country_name','goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name','base_category.name as category_name')
                    ->join('users', 'goods.users_id', '=', 'users.id')
                    ->leftJoin('base_category', 'goods.category', '=', 'base_category.id')
                    ->leftJoin('base_country', 'goods.country_id', '=', 'base_country.id')
                    ->leftJoin('base_province', 'goods.province_id', '=', 'base_province.code')
                    ->leftJoin('base_city', 'goods.city_id', '=', 'base_city.code')
                    ->leftJoin('base_area', 'goods.area_id', '=', 'base_area.code')
                    ->where($where)
                    ->whereIn('goods.search_goods',$whereIn)
                    ->orWhere($orWhere)
                    ->orderBy('goods.id', 'desc')->skip($pyl)->take($pageSize)->get();
            }elseif(!empty($whereBeween) && !empty($whereIn)){
                $res = DB::table('goods')->select('base_area.name as area_name','base_city.name as city_name','base_province.name as province_name','base_country.name as country_name','goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name','base_category.name as category_name')
                    ->join('users', 'goods.users_id', '=', 'users.id')
                    ->leftJoin('base_category', 'goods.category', '=', 'base_category.id')
                    ->leftJoin('base_country', 'goods.country_id', '=', 'base_country.id')
                    ->leftJoin('base_province', 'goods.province_id', '=', 'base_province.code')
                    ->leftJoin('base_city', 'goods.city_id', '=', 'base_city.code')
                    ->leftJoin('base_area', 'goods.area_id', '=', 'base_area.code')
                    ->where($where)
                    ->whereBetween('goods.pay_diamond',$whereBeween)
                    ->whereIn('goods.search_goods',$whereIn)
                    ->orWhere($orWhere)
                    ->orderBy('goods.id', 'desc')->skip($pyl)->take($pageSize)->get();
            }else{
                $res = DB::table('goods')->select('base_area.name as area_name','base_city.name as city_name','base_province.name as province_name','base_country.name as country_name','goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name','base_category.name as category_name')
                    ->join('users', 'goods.users_id', '=', 'users.id')
                    ->leftJoin('base_category', 'goods.category', '=', 'base_category.id')
                    ->leftJoin('base_country', 'goods.country_id', '=', 'base_country.id')
                    ->leftJoin('base_province', 'goods.province_id', '=', 'base_province.code')
                    ->leftJoin('base_city', 'goods.city_id', '=', 'base_city.code')
                    ->leftJoin('base_area', 'goods.area_id', '=', 'base_area.code')
                    ->where($where)
                    ->orWhere($orWhere)
                    ->orderBy('goods.id', 'desc')->skip($pyl)->take($pageSize)->get();
            }
            if(!empty($res)){
                //查询评论信息
                foreach($res as $k=>$v){
                    $displayArea = '全球';
                    if($v->country_name){
                        $displayArea .= '>'.$v->country_name;
                    }
                    if($v->province_name){
                        $displayArea .= '>'.$v->province_name;
                    }
                    if($v->city_name){
                        $displayArea .= '>'.$v->city_name;
                    }
                    if($v->area_name){
                        $displayArea .= '>'.$v->area_name;
                    }
                    $v->display_area = $displayArea;
                    unset($v->country_name);
                    unset($v->province_name);
                    unset($v->city_name);
                    unset($v->area_name);
                    //判断当前需求是否是自己，自己--显示所有评论数据，反之--自己评论数据
                    if($v->users_id == $userId){
                        $v->comment= DB::table('user_message')->select('user_message.id','user_message.message','user_message.price','user_message.pay_diamond','user_message.create_time','users.head_pic','users.nick_name','users.id as users_id')
                            ->join('users', 'user_message.users_id', '=', 'users.id')
                            ->where([['user_message.status','=',1],['user_message.is_del','=',2],['user_message.goods_id','=',$v->id]])
                            ->orderBy('user_message.pay_diamond','desc')->get();
                    }else{
                        $v->comment= DB::table('user_message')->select('user_message.id','user_message.message','user_message.price','user_message.pay_diamond','user_message.create_time','users.head_pic','users.nick_name','users.id as users_id')
                            ->join('users', 'user_message.users_id', '=', 'users.id')
                            ->where([['user_message.status','=',1],['user_message.is_del','=',2],['user_message.goods_id','=',$v->id],['user_message.users_id','=',$userId]])
                            ->orderBy('user_message.pay_diamond','desc')->get();
                    }
                }
            }
            return response()->json(['success'=>true,'code'=>1000,'list'=>$res]);
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    /**
     * 获取找货详情
     *
     * @param  Request
     * @return Response
     */
    public function infoGoods(Request $request)
    {
        try{
            $userId = $request->input('userId', '');
            $goodsId = $request->input('goodsId', '');
            if(empty($userId) || empty($goodsId)){
                return response()->json(['success'=>false,'code'=>2017,'message'=>'必填参数不能为空']);
            }
            $res = DB::table('goods')->select('base_area.name as area_name','base_city.name as city_name','base_province.name as province_name','base_country.name as country_name','goods.id','goods.users_id','goods.name','goods.pic','goods.pay_diamond','goods.create_time','users.head_pic','users.nick_name','base_category.name as category_name')
                ->join('users', 'goods.users_id', '=', 'users.id')
                ->leftJoin('base_category', 'goods.category', '=', 'base_category.id')
                ->leftJoin('base_country', 'goods.country_id', '=', 'base_country.id')
                ->leftJoin('base_province', 'goods.province_id', '=', 'base_province.code')
                ->leftJoin('base_city', 'goods.city_id', '=', 'base_city.code')
                ->leftJoin('base_area', 'goods.area_id', '=', 'base_area.code')
                ->where('goods.id','=',$goodsId)
                ->first();
            if(!empty($res)){
                //判断当前需求是否是自己，自己--显示所有评论数据，反之--自己评论数据
                $displayArea = '全球';
                if($res->country_name){
                    $displayArea .= '>'.$res->country_name;
                }
                if($res->province_name){
                    $displayArea .= '>'.$res->province_name;
                }
                if($res->city_name){
                    $displayArea .= '>'.$res->city_name;
                }
                if($res->area_name){
                    $displayArea .= '>'.$res->area_name;
                }
                $res->display_area = $displayArea;
                unset($res->country_name);
                unset($res->province_name);
                unset($res->city_name);
                unset($res->area_name);
                if($res->users_id == $userId){
                    $res->comment= DB::table('user_message')->select('user_message.id','user_message.message','user_message.price','user_message.pay_diamond','user_message.create_time','users.head_pic','users.nick_name','users.id as users_id')
                        ->join('users', 'user_message.users_id', '=', 'users.id')
                        ->where([['user_message.status','=',1],['user_message.is_del','=',2],['user_message.goods_id','=',$res->id]])
                        ->orderBy('user_message.pay_diamond','desc')->get();
                }else{
                    $res->comment= DB::table('user_message')->select('user_message.id','user_message.message','user_message.price','user_message.pay_diamond','user_message.create_time','users.head_pic','users.nick_name','users.id as users_id')
                        ->join('users', 'user_message.users_id', '=', 'users.id')
                        ->where([['user_message.status','=',1],['user_message.is_del','=',2],['user_message.goods_id','=',$res->id],['user_message.users_id','=',$userId]])
                        ->orderBy('user_message.pay_diamond','desc')->get();
                }
            }
            return response()->json(['success'=>true,'code'=>1000,'list'=>$res]);
        }catch (\Exception $e){
            return response()->json(['success'=>false,'code'=>9999,'message'=>$e->getMessage()]);
        }
    }

    public function httpGet($url){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }
}
